package com.ikkong.school.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.school.model.School;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.school.model.Cookbook;
import com.ikkong.school.service.CookbookService;
import com.ikkong.school.service.impl.CookbookServiceImpl;
import org.beetl.sql.core.kit.StringKit;

/**
 * Generated by Blade.
 * 2018-07-22 21:09:41
 */
public class CookbookController extends UrlPermissController {
	private static String CODE = "cookbook";
	private static String PERFIX = "cookbook";
	private static String LIST_SOURCE = "Cookbook.list";
	private static String BASE_PATH = "/platform/cookbook/";
	
	CookbookService service = new CookbookServiceImpl();
	
	public void index() {
        String roles = ShiroKit.getUser().getRoles().toString();
        int i = 0;
        if(roles.contains("2") || roles.contains("1")){
            i = 1;
        }
        setAttr("isadmin",i);
        setAttr("code", CODE);
		render(BASE_PATH + "cookbook.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "cookbook_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Cookbook cookbook = service.findById(id);
		setAttr("model", JsonKit.toJson(cookbook));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "cookbook_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Cookbook cookbook = service.findById(id);
		setAttr("model", JsonKit.toJson(cookbook));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "cookbook_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		Cookbook cookbook = mapping(PERFIX, Cookbook.class);
        cookbook.setStatus(0);
        String deptId = ShiroKit.getUser().getDeptId().toString();
        School school = Blade.dao().selectSingle("School.bydept", Record.create().set("deptid", deptId), School.class);
        if(school==null){
            renderJson(error("没有找到该学校"));
            return;
        }

        cookbook.setS_id(school.getId());
        boolean temp = service.save(cookbook);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Cookbook cookbook = mapping(PERFIX, Cookbook.class);
		boolean temp = service.update(cookbook);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
        int i = 0;
		if(StringKit.isNotBlank(ids)){
            List<String> idli = Arrays.asList(ids.split(","));
            for(String id:idli){
                if(StringKit.isNotBlank(id)){
                    Cookbook byId = service.findById(id);
                    if(byId!=null){
                        byId.setStatus(1);
                        boolean update = service.update(byId);
                        if(update){
                            i++;
                        }
                    }
                }
            }
        }
		if (i > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
